using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using WebChart;


namespace OtherWinDemo
{
	public class Form1 : System.Windows.Forms.Form
	{
		#region Windows Stuff
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.Button button1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.button1 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// pictureBox1
			// 
			this.pictureBox1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.pictureBox1.Location = new System.Drawing.Point(8, 8);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(616, 400);
			this.pictureBox1.TabIndex = 0;
			this.pictureBox1.TabStop = false;
			this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
			// 
			// button1
			// 
			this.button1.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.button1.Location = new System.Drawing.Point(40, 424);
			this.button1.Name = "button1";
			this.button1.TabIndex = 1;
			this.button1.Text = "Add Chart";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(632, 454);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.button1,
																		  this.pictureBox1});
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		#endregion

		ChartEngine engine;
		public Form1() {
			InitializeComponent();
			InitializeCharts();
		}

		private ChartEngine CreateEngine() {
			ChartEngine engine = new ChartEngine();
			engine.Charts = new ChartCollection(engine);
			engine.Background = new ChartInterior(Color.White);
			engine.PlotBackground = new ChartInterior(Color.White);
			engine.Legend.Position = LegendPosition.Bottom;
			engine.Legend.Width = 50;
			engine.HasChartLegend = true;
			engine.GridLines = GridLines.Horizontal;
			StringFormat format = new StringFormat();
			format.Alignment = StringAlignment.Far;
			format.FormatFlags=StringFormatFlags.LineLimit;
			engine.YAxisFont = new ChartText(format);
			return engine;
		}
		private void InitializeCharts() {
			engine = CreateEngine();
			engine.Charts.Add( AddChart() );
		}
		
		private Chart AddChart() {
			Random rnd = new Random();			

			Chart c = new SmoothLineChart();
			for (int i=0; i < 30; i++) {
				int val = rnd.Next(100);
				c.Data.Add( new ChartPoint( val.ToString(), val ));
			}
			c.Line.Color = Color.Red;

			c.Fill.Color = Color.FromArgb(rnd.Next(255),rnd.Next(255),rnd.Next(255));
			
			int lm = rnd.Next(3);
			if (lm == 0)
				c.LineMarker = new CircleLineMarker(6, c.Fill.Color, c.Line.Color);
			else if (lm==1)
				c.LineMarker = new TriangleLineMarker(6, c.Fill.Color, c.Line.Color);
			else if (lm==2)
				c.LineMarker = new DiamondLineMarker(6, c.Fill.Color, c.Line.Color);
			else if (lm==3)
				c.LineMarker = new XLineMarker(6, c.Fill.Color, c.Line.Color);

			c.Legend = "Chart: " + engine.Charts.Count.ToString();

			return c;
		}
		private void button1_Click(object sender, System.EventArgs e) {
			engine.Charts.Add( AddChart() );
			pictureBox1.Invalidate();
 		}

		private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {
			engine.Size = pictureBox1.Size;
			pictureBox1.Image = engine.GetBitmap();
		}
	}
}
